package edu.tamu.cse.teamproject1.Database;

/**
 * 
 * @author Ben Unsworth
 * This creates a variable length string
 */

public class varchar {
	private String varString;
	int size;
	
	/**
	 * constructor
	 * defines the variable string
	 * @param newString
	 */
	varchar(String newString){
		varString = newString;
		size = newString.length();
	}
	
	/**
	 * constructor
	 * defines the variable string and its size
	 * @param newString, newSize
	 */
	varchar(String newString, int newSize){
		setString(newString, newSize);
	}

	/**
	 * constructor
	 * defines an empty variable string and its size
	 * @param newSize
	 */
	varchar(int newSize){
		size = newSize;
	}
	
	/**
	 * 
	 * @return string as value for TupleElement
	 */
	String getString(){
		return varString;
	}
	
	/**
	 * sets the variable string, cuts off end if
	 * string is too large
	 * @param newString
	 */
	void setString(String newString){
		setString(newString, size);
	}
	
	/**
	 * sets the variable string and size
	 * also cuts off end if string is too large
	 * @param newString, newSize
	 */
	void setString(String newString, int newSize){
		if(newString.length() > newSize){
			varString = newString.substring(0, newSize);
		}else{
			varString = newString;
		}
		size = newSize;
	}
	
	/**
	 * resizes the variable string
	 * @param newSize
	 */
	void resize(int newSize){
		setString(varString, newSize);
	}
	
	/**
	 * returns the size of this variable string
	 * @return size of varchar
	 */
	int getSize(){
		return size;
	}
	
	/**
	 * Concatenates to the end of the variable string
	 * @param newString
	 */
	void concatString(String newString){
		varString.concat(newString);
		setString(varString, size);
	}
	
	/**
	 * Provides the overriding of the toString method
	 * 
	 * @author Christopher Weldon <chris@chrisweldon.net>
	 * @return String equivalent of this object
	 */
	@Override
	public String toString() {
		return this.varString;
	}
}
